// 1.创建contextMenus
var contextMenu = {
    id: "addAmount",
    title: "添加消费",
    // contexts:page,video....
    contexts: ["selection"]
}
chrome.contextMenus.create(contextMenu);
// 2.为contextMenus添加点击事件的监听
chrome.contextMenus.onClicked.addListener(function (clickData) {
    // clickData.menuItemId:被点击的菜单选项卡的id
    // clickData.selectionText:选中的内容
    if (clickData.menuItemId == "addAmount" && clickData.selectionText) {
        var amount = parseFloat(clickData.selectionText);
        // 1.从浏览器中获取存储的金额
        chrome.storage.sync.get(['total', 'limit'], function (budget) {
            var totalAmount = 0;
            if (budget.total) {
                totalAmount = parseFloat(budget.total);
            }
            // 2.将本次金额加到总金额并存储
            if (amount) {
                totalAmount += amount;
                if (totalAmount > parseFloat(budget.limit)) {
                    var notifyOptions = {
                        // type:basic,imge,simple,list
                        type: 'basic',
                        title: '金额超出限制',
                        iconUrl: 'img/logo.png',
                        message: '您消费的总金额已超出上限'
                    }
                    chrome.notifications.create('limitNotify', notifyOptions);
                } else {
                    chrome.storage.sync.set({ 'total': totalAmount });
                }

            }
        });
    }
});